package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.TiposIntervencao;

import com.model.Agenda;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table AGENDA.
*/
public class AgendaDao extends AbstractDao<Agenda, Long> {

    public static final String TABLENAME = "AGENDA";

    /**
     * Properties of entity Agenda.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_idAgenda");
        public final static Property Grupoact = new Property(1, Integer.class, "grupoact", false, "GRUPOACT");
        public final static Property Data = new Property(2, Integer.class, "Data", false, "DATA");
        public final static Property Dia = new Property(3, Integer.class, "Dia", false, "DIA");
        public final static Property Mes = new Property(4, Integer.class, "Mes", false, "MES");
        public final static Property Datanormal = new Property(5, String.class, "Datanormal", false, "DATANORMAL");
        public final static Property Hora = new Property(6, String.class, "Hora", false, "HORA");
        public final static Property Tarefa = new Property(7, String.class, "Tarefa", false, "TAREFA");
        public final static Property Observacoes = new Property(8, String.class, "Observacoes", false, "OBSERVACOES");
        public final static Property Concluido = new Property(9, boolean.class, "Concluido", false, "CONCLUIDO");
        public final static Property Lembrarantes1 = new Property(10, Integer.class, "Lembrarantes1", false, "LEMBRARANTES1");
        public final static Property Dataalerta = new Property(11, String.class, "Dataalerta", false, "DATAALERTA");
        public final static Property Horaalerta = new Property(12, String.class, "Horaalerta", false, "HORAALERTA");
        public final static Property Alertaautomatico = new Property(13, Boolean.class, "Alertaautomatico", false, "ALERTAAUTOMATICO");
        public final static Property Isnote = new Property(14, Boolean.class, "isnote", false, "ISNOTE");
        public final static Property Updateinsert = new Property(15, Boolean.class, "updateinsert", false, "UPDATEINSERT");
        public final static Property Codigoapicultor = new Property(16, long.class, "Codigoapicultor", false, "CODIGOAPICULTOR");
        public final static Property Cdapiario = new Property(17, long.class, "cdapiario", false, "CDAPIARIO");
        public final static Property TipoTarefa = new Property(18, long.class, "tipoTarefa", false, "TIPO_TAREFA");
    };

    private DaoSession daoSession;

    private Query<Agenda> apicultor_AgendaApicultorQuery;
    private Query<Agenda> apiario_AgendaApiarioQuery;
    private Query<Agenda> tiposIntervencao_AgendaTintervencaoQuery;

    public AgendaDao(DaoConfig config) {
        super(config);
    }
    
    public AgendaDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'AGENDA' (" + //
                "'_idAgenda' INTEGER PRIMARY KEY ASC AUTOINCREMENT ," + // 0: id
                "'GRUPOACT' INTEGER," + // 1: grupoact
                "'DATA' INTEGER," + // 2: Data
                "'DIA' INTEGER," + // 3: Dia
                "'MES' INTEGER," + // 4: Mes
                "'DATANORMAL' TEXT," + // 5: Datanormal
                "'HORA' TEXT," + // 6: Hora
                "'TAREFA' TEXT NOT NULL ," + // 7: Tarefa
                "'OBSERVACOES' TEXT," + // 8: Observacoes
                "'CONCLUIDO' INTEGER NOT NULL ," + // 9: Concluido
                "'LEMBRARANTES1' INTEGER," + // 10: Lembrarantes1
                "'DATAALERTA' TEXT," + // 11: Dataalerta
                "'HORAALERTA' TEXT," + // 12: Horaalerta
                "'ALERTAAUTOMATICO' INTEGER," + // 13: Alertaautomatico
                "'ISNOTE' INTEGER," + // 14: isnote
                "'UPDATEINSERT' INTEGER," + // 15: updateinsert
                "'CODIGOAPICULTOR' INTEGER NOT NULL ," + // 16: Codigoapicultor
                "'CDAPIARIO' INTEGER NOT NULL ," + // 17: cdapiario
                "'TIPO_TAREFA' INTEGER NOT NULL );"); // 18: tipoTarefa
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'AGENDA'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Agenda entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Integer grupoact = entity.getGrupoact();
        if (grupoact != null) {
            stmt.bindLong(2, grupoact);
        }
 
        Integer Data = entity.getData();
        if (Data != null) {
            stmt.bindLong(3, Data);
        }
 
        Integer Dia = entity.getDia();
        if (Dia != null) {
            stmt.bindLong(4, Dia);
        }
 
        Integer Mes = entity.getMes();
        if (Mes != null) {
            stmt.bindLong(5, Mes);
        }
 
        String Datanormal = entity.getDatanormal();
        if (Datanormal != null) {
            stmt.bindString(6, Datanormal);
        }
 
        String Hora = entity.getHora();
        if (Hora != null) {
            stmt.bindString(7, Hora);
        }
        stmt.bindString(8, entity.getTarefa());
 
        String Observacoes = entity.getObservacoes();
        if (Observacoes != null) {
            stmt.bindString(9, Observacoes);
        }
        stmt.bindLong(10, entity.getConcluido() ? 1l: 0l);
 
        Integer Lembrarantes1 = entity.getLembrarantes1();
        if (Lembrarantes1 != null) {
            stmt.bindLong(11, Lembrarantes1);
        }
 
        String Dataalerta = entity.getDataalerta();
        if (Dataalerta != null) {
            stmt.bindString(12, Dataalerta);
        }
 
        String Horaalerta = entity.getHoraalerta();
        if (Horaalerta != null) {
            stmt.bindString(13, Horaalerta);
        }
 
        Boolean Alertaautomatico = entity.getAlertaautomatico();
        if (Alertaautomatico != null) {
            stmt.bindLong(14, Alertaautomatico ? 1l: 0l);
        }
 
        Boolean isnote = entity.getIsnote();
        if (isnote != null) {
            stmt.bindLong(15, isnote ? 1l: 0l);
        }
 
        Boolean updateinsert = entity.getUpdateinsert();
        if (updateinsert != null) {
            stmt.bindLong(16, updateinsert ? 1l: 0l);
        }
        stmt.bindLong(17, entity.getCodigoapicultor());
        stmt.bindLong(18, entity.getCdapiario());
        stmt.bindLong(19, entity.getTipoTarefa());
    }

    @Override
    protected void attachEntity(Agenda entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Agenda readEntity(Cursor cursor, int offset) {
        Agenda entity = new Agenda( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1), // grupoact
            cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2), // Data
            cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3), // Dia
            cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4), // Mes
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // Datanormal
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // Hora
            cursor.getString(offset + 7), // Tarefa
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // Observacoes
            cursor.getShort(offset + 9) != 0, // Concluido
            cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10), // Lembrarantes1
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // Dataalerta
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // Horaalerta
            cursor.isNull(offset + 13) ? null : cursor.getShort(offset + 13) != 0, // Alertaautomatico
            cursor.isNull(offset + 14) ? null : cursor.getShort(offset + 14) != 0, // isnote
            cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0, // updateinsert
            cursor.getLong(offset + 16), // Codigoapicultor
            cursor.getLong(offset + 17), // cdapiario
            cursor.getLong(offset + 18) // tipoTarefa
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Agenda entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setGrupoact(cursor.isNull(offset + 1) ? null : cursor.getInt(offset + 1));
        entity.setData(cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2));
        entity.setDia(cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3));
        entity.setMes(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4));
        entity.setDatanormal(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setHora(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setTarefa(cursor.getString(offset + 7));
        entity.setObservacoes(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setConcluido(cursor.getShort(offset + 9) != 0);
        entity.setLembrarantes1(cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10));
        entity.setDataalerta(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setHoraalerta(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setAlertaautomatico(cursor.isNull(offset + 13) ? null : cursor.getShort(offset + 13) != 0);
        entity.setIsnote(cursor.isNull(offset + 14) ? null : cursor.getShort(offset + 14) != 0);
        entity.setUpdateinsert(cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0);
        entity.setCodigoapicultor(cursor.getLong(offset + 16));
        entity.setCdapiario(cursor.getLong(offset + 17));
        entity.setTipoTarefa(cursor.getLong(offset + 18));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Agenda entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Agenda entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "agendaApicultor" to-many relationship of Apicultor. */
    public List<Agenda> _queryApicultor_AgendaApicultor(long Codigoapicultor) {
        synchronized (this) {
            if (apicultor_AgendaApicultorQuery == null) {
                QueryBuilder<Agenda> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Codigoapicultor.eq(null));
                apicultor_AgendaApicultorQuery = queryBuilder.build();
            }
        }
        Query<Agenda> query = apicultor_AgendaApicultorQuery.forCurrentThread();
        query.setParameter(0, Codigoapicultor);
        return query.list();
    }

    /** Internal query to resolve the "agendaApiario" to-many relationship of Apiario. */
    public List<Agenda> _queryApiario_AgendaApiario(long cdapiario) {
        synchronized (this) {
            if (apiario_AgendaApiarioQuery == null) {
                QueryBuilder<Agenda> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Cdapiario.eq(null));
                apiario_AgendaApiarioQuery = queryBuilder.build();
            }
        }
        Query<Agenda> query = apiario_AgendaApiarioQuery.forCurrentThread();
        query.setParameter(0, cdapiario);
        return query.list();
    }

    /** Internal query to resolve the "agendaTintervencao" to-many relationship of TiposIntervencao. */
    public List<Agenda> _queryTiposIntervencao_AgendaTintervencao(long tipoTarefa) {
        synchronized (this) {
            if (tiposIntervencao_AgendaTintervencaoQuery == null) {
                QueryBuilder<Agenda> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.TipoTarefa.eq(null));
                tiposIntervencao_AgendaTintervencaoQuery = queryBuilder.build();
            }
        }
        Query<Agenda> query = tiposIntervencao_AgendaTintervencaoQuery.forCurrentThread();
        query.setParameter(0, tipoTarefa);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getTiposIntervencaoDao().getAllColumns());
            builder.append(" FROM AGENDA T");
            builder.append(" LEFT JOIN APICULTOR T0 ON T.'CODIGOAPICULTOR'=T0.'_idApicultor'");
            builder.append(" LEFT JOIN APIARIO T1 ON T.'CDAPIARIO'=T1.'_idApiario'");
            builder.append(" LEFT JOIN TIPOS_INTERVENCAO T2 ON T.'TIPO_TAREFA'=T2.'_idTiposIntervencao'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Agenda loadCurrentDeep(Cursor cursor, boolean lock) {
        Agenda entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        TiposIntervencao tiposIntervencao = loadCurrentOther(daoSession.getTiposIntervencaoDao(), cursor, offset);
         if(tiposIntervencao != null) {
            entity.setTiposIntervencao(tiposIntervencao);
        }

        return entity;    
    }

    public Agenda loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Agenda> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Agenda> list = new ArrayList<Agenda>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Agenda> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Agenda> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
